﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages
{
    public class SymptomStat : YouHP.Pages.BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int type = RequestTools.GetInt("type", 0);

            if (1 == type )
            {
                MakeSeveritiesGraph();
            }
            else if (2 == type)
            {
                MakeDiseaseGraph();
            }
        }

        private void MakeSeveritiesGraph()
        {
            int sid = RequestTools.GetQueryInt("sid", 0);
            if (sid <= 0)
            {
                return;
            }

            List<SymptomStatSeverityCount> lst = SymptomBLL.Instance.GetSymptomStatSeverity(sid);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("&PieStyle=1&\r\n");
            int i = 0;
            foreach (SymptomStatSeverityCount info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, info.SeverityName, info.colorStr, info.PatientCount);
                i++;
            }

            sb.AppendFormat("&Var=症状程度统计;{0}&;\r\n", lst.Count);
            
            ResponseXML(sb);
        }

        private void MakeDiseaseGraph()
        {
            int sid = RequestTools.GetQueryInt("sid", 0);
            if (sid <= 0) 
                return;

            List<SymptomStatDiseaseCount> lst = SymptomBLL.Instance.GetSymptomStatDisease(sid);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("&Bar=1&\r\n");

            int i = 0;
            string nameStr = "";
            string linkStr = "";

            foreach (SymptomStatDiseaseCount info in lst)
            {
                nameStr = string.Format("<a href='{0}'>{1}</a>", GetUrl("/Disease.aspx?id=" + info.DiseaseId), info.DiseaseName);
                if (info.PatientCount > 0)
                {
                    linkStr = GetUrl("/PatientList.aspx?dszid=" + info.DiseaseId);
                }
                else
                {
                    linkStr = "";
                }
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, nameStr, linkStr, info.PatientCount);
                i++;
            }

            sb.AppendFormat("&Var=症状疾病分布;{0}&;\r\n", lst.Count);

            ResponseXML(sb);
       }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}